class Book:
    def __init__(self, title, pages):
        self.title = title
        self.pages = pages

    def __len__(self):
        return self.pages

    def __str__(self):
        return f"'{self.title}' has {self.pages} pages"

    def __eq__(self, other):
        return self.pages == other.pages


# book1 = Book("Built Wealth Like a Boss", 420)
# book2 = Book("Be Your Own Start", 420)
#
# print(len(book1))  # 420
# print(len(book2))  # 420
# print(str(book1))  # 'Built Wealth Like a Boss' has 420 pages
# print(str(book2))  # 'Be Your Own Start' has 420 pages
# print(book1 == book2)  # True

class Cart:
   def __init__(self):
       self.items = []

   def add(self, item):
       self.items.append(item)

   def remove(self, item):
       if item in self.items:
           self.items.remove(item)
       else:
           print(f'{item} is not in cart')

   def list_items(self):
       return self.items

   def __len__(self):
       return len(self.items)

   def __getitem__(self, index):
       """中括号表示法，通过索引获取值"""
       return self.items[index]

   def __contains__(self, item):
       """in 操作，判断item是否在Cart中"""
       return item in self.items

   def __iter__(self):
       """for循环遍历"""
       return iter(self.items)

cart = Cart()
cart.add('Laptop')
cart.add('Wireless mouse')
cart.add('Ergo keyboard')
cart.add('Monitor')

for item in cart:
   print(item, end=' ') # Laptop Wireless mouse Ergo keyboard Monitor

print(len(cart)) # 4
print(cart[3]) # Monitor

print('Monitor' in cart) # True
print('banana' in cart) # False

cart.remove('Ergo keyboard')

print(cart.list_items()) # ['Laptop', 'Wireless mouse', 'Monitor']

cart.remove('banana') # banana is not in cart